import { ref, computed } from 'vue';
import { defineStore } from 'pinia';
import { useLocalStorage } from '@vueuse/core';

/**
 * 菜单数据存储
 * @returns {Object}
 */
export const useMenusStore = defineStore('menus', () => {
    // 本地存储菜单数据
    const menus = useLocalStorage('MENUS', null)
    // 是否有菜单数据
    const isMenu = computed(() => menus.value !== null)
    // 菜单数据
    const menuList = computed(() => JSON.parse(menus.value))
    // 面包屑列表
    const breadcrumbList = ref([])
    // tags标签列表
    const tagsList = useLocalStorage('TAGS', [])
    // 菜单组件
    const getViews = computed(() => import.meta.glob('@/views/**/index.vue'))

    // 本地存储菜单数据
    function setMenus(datamenus) {
        menus.value = JSON.stringify(datamenus)
    }

    // 清除菜单数据
    function clearMenus() {
        menus.value = null
    }

    // 组装路由显示结构
    const getRoutes = computed(() => {
        let routes = [
            {
                path: '/layout',
                redirect: '/dashboard', 
                name: 'layout',
                component: () => import('@/layout/index.vue'),
                children: []
            }
        ] 
        let _menuList = menuList.value || []
        for(let i = 0; i < _menuList.length; i++) {
            if (_menuList[i].meta.fullpage) {
                routes.push(_menuList[i])
            } else {
                routes[0].children.push(_menuList[i])
            }
        }
        return routes
    })

    // 加载菜单组件
    function loadView(viewname){
        if (viewname) {
            for (let i = 0; i < Object.values(getViews.value).length; i++) {
                const item = Object.values(getViews.value)[i]
                const path = item.toString().split('views/')[1].split('/index.vue')[0]
                if (path === viewname) {
                  return item
                }
            }
        }
        return () => import('@/layout/other/NotFoundView.vue')
    }

    // 菜单数据映射菜单组件方法
    function treeLoadCommponent(menusData) {
        return menusData.map(item => {
            if (item.component && item.name !== 'layout') {
                item.component = loadView(item.component)
            }
            if (item.children.length > 0) {
                item.children = treeLoadCommponent(item.children)
            }
            return item
        })
    }

    /**
     * 菜单数据映射组件
     */
    const getLoadCommponentMenusData = computed(() => {
        return treeLoadCommponent(getRoutes.value)
    })

    /**
     * 菜单数据动态添加路由
     */
    function treeMenusDataAddRouter(router){
        let _menus = getLoadCommponentMenusData.value
        _menus.forEach(item => {
            if (!router.hasRoute(item.path)){
                router.addRoute(item)
            }
        }) 
    }

    // 平铺菜单数据
    function treeMenusDataFlat(menusData) {
        let _menus = []
        menusData.forEach(item => {
            _menus.push(item)
            if (item.children.length > 0) {
                _menus = _menus.concat(treeMenusDataFlat(item.children))
            }
        })
        return _menus
    }
    // 动态设置面包屑列表
    function setBreadcrumb(keys) {
        let _menus = treeMenusDataFlat(menuList.value)
        breadcrumbList.value = []
        _menus.forEach(item => {
            if (keys.includes(item.id)) {
                breadcrumbList.value.push(item)
            }
        })
    }

    // 设置标签列表
    function setTags(tag) {
        let hasRoute = tagsList.value.some(item => item.path === tag.path && item.name === tag.name)
        if (!hasRoute) {
            tagsList.value.push(tag)
        }
    }
    // 删除tag
    function removeTags(tag) {
        tagsList.value.forEach((item, index) => {
            if (item.path === tag.path && item.name === tag.name) {
                tagsList.value.splice(index, 1)
            }
        })
    }
    // 清理tags
    function clearTags() {
        tagsList.value = []
    }

    return {
        menus, isMenu, menuList, breadcrumbList, tagsList,
        setMenus, treeMenusDataAddRouter, clearMenus, setBreadcrumb,
        setTags, removeTags, clearTags
    }

})